package com.ruoyi.employee.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.employee.domain.Departments;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.employee.domain.Socialsecurity;
import com.ruoyi.employee.mapper.EmployeeMapper;
import com.ruoyi.employee.domain.Employee;
import com.ruoyi.employee.service.IEmployeeService;

/**
 * 员工信息Service业务层处理
 * 
 * @author Mr.阚
 * @date 2025-02-25
 */
@Service
public class EmployeeServiceImpl implements IEmployeeService 
{
    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 查询员工信息
     * 
     * @param id 员工信息主键
     * @return 员工信息
     */
    @Override
    public Employee selectEmployeeById(Long id)
    {
        return employeeMapper.selectEmployeeById(id);
    }

    /**
     * 查询员工信息列表
     * 
     * @param employee 员工信息
     * @return 员工信息
     */
    @Override
    public List<Employee> selectEmployeeList(Employee employee)
    {
        return employeeMapper.selectEmployeeList(employee);
    }

    /**
     * 新增员工信息
     * 
     * @param employee 员工信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertEmployee(Employee employee)
    {
        int rows = employeeMapper.insertEmployee(employee);
        insertSocialsecurity(employee);
        return rows;
    }

    /**
     * 修改员工信息
     * 
     * @param employee 员工信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateEmployee(Employee employee)
    {
        employeeMapper.deleteSocialsecurityByAccountSetsId(employee.getId());
        insertSocialsecurity(employee);
        return employeeMapper.updateEmployee(employee);
    }

    /**
     * 批量删除员工信息
     * 
     * @param ids 需要删除的员工信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteEmployeeByIds(Long[] ids)
    {
        employeeMapper.deleteSocialsecurityByAccountSetsIds(ids);
        return employeeMapper.deleteEmployeeByIds(ids);
    }

    /**
     * 删除员工信息信息
     * 
     * @param id 员工信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteEmployeeById(Long id)
    {
        employeeMapper.deleteSocialsecurityByAccountSetsId(id);
        return employeeMapper.deleteEmployeeById(id);
    }
//批量修改离职状态
    @Override
    public int batchLeave(List<Long> ids) {
         if (ids == null || ids.isEmpty()) {
            throw new IllegalArgumentException("员工ID列表不能为空");
        }

        // 调用 Mapper 批量更新
       return employeeMapper.batchLeave(ids);


    }



    @Override
    public int updateAccrualsubject(List<Long> ids, String accrualsubject) {
        if (ids == null) {
            throw new IllegalArgumentException("员工ID不能为空");
        }
        if (accrualsubject == null) {
            throw new IllegalArgumentException("计提科目不能为空");
        }
        // 调用 Mapper 更新
        return  employeeMapper.updateAccrualSubjectById(ids,  accrualsubject);

    }



    @Override
    public int addDepartments(Departments departments) {
        return employeeMapper.addDepartments(departments);
    }

    /**
     * 新增五险一金信息
     * 
     * @param employee 员工信息对象
     */
    public void insertSocialsecurity(Employee employee)
    {
        List<Socialsecurity> socialsecurityList = employee.getSocialsecurityList();
        Long id = employee.getId();
        if (StringUtils.isNotNull(socialsecurityList))
        {
            List<Socialsecurity> list = new ArrayList<Socialsecurity>();
            for (Socialsecurity socialsecurity : socialsecurityList)
            {
                socialsecurity.setAccountSetsId(id);
                list.add(socialsecurity);
            }
            if (list.size() > 0)
            {
                employeeMapper.batchSocialsecurity(list);
            }
        }
    }
}
